[deps]
musl
kernel-headers

[vars]
filesize=1045868
sha512=38108a94118b56c6f3982e692beb1068c52f9aecd74447d6b552b77fae5e365eab72b4781bdf07d9696ac89d54371c1cae8cc0363de510b53988302acef50dba

[mirrors]
http://dl.suckless.org/tools/9base-6.tar.gz

[build]
mbwc_fix() {
        local file=$1
        echo '#include "mbwc.h"' > $file
        cat troff/$file >> $file
        mv $file troff/$file
}
for i in n1.c t11.c n10.c mbwc.c ; do mbwc_fix $i ; done

echo "PREFIX = $butch_prefix" >>config.mk
# the dc calculator, called by bc, assumes a signed char,
# which is not true for ARM. it fails on "4 - 3" resp. "4 3 -ps."
# without -fsigned-char.
echo "CFLAGS += -fsigned-char -D_GNU_SOURCE -DPLAN9PORT -D__USE_MISC $optcflags" >>config.mk
echo "LDFLAGS += $optldflags" >> config.mk
echo "CC = $CC" >> config.mk
echo "STRIP = true" >> config.mk
if [ -n "$CROSS_COMPILE" ] ; then
	echo "YACC = yacc" >> config.mk
	patch -p1 < "$K"/9base-bc-yacc.patch
fi

for i in diff rc lib9 sam ; do sed -i 's@-I\${PREFIX}/include@@' $i/Makefile ; done
for i in std.mk diff/Makefile sam/Makefile ; do
  sed -i 's/@strip/$(STRIP)/' $i
done

sed -i 's/__linux__/__not_linux__/;s/__sun__/__linux__/' lib9/dirread.c
sed -i '/sys.termios.h/d' lib9/readcons.c
sed -i '/^.touch./d' Makefile  # we don't have futimes

# ed's getline clobbers the one defined in musl, and this causes problems
# when libc functions like getpwuid call getline.
patch -p1 < "$K"/9base-ed-getline.patch

stuff_to_build="yacc ascii bc cleanname dc ed factor fmt freq getflags hoc join \
look mk mtime pbd primes rc read sam troff unicode"

sed -i 's/SUBDIRS  =/FOO =/' Makefile
[ -n "$CROSS_COMPILE" ] && \
  stuff_to_build=$(echo "$stuff_to_build" | sed -e 's/hoc //' -e 's/yacc //')
echo "SUBDIRS=lib9 $stuff_to_build" >> config.mk

make -j$MAKE_THREADS

# only install things not sufficently well-provided by busybox
for f in ascii bc cleanname dc ed factor fmt freq getflags hoc join \
         look mk mtime pbd primes rc read sam troff unicode
do
  if [ "$f" = hoc ] && [ -n "$CROSS_COMPILE" ] ; then :
  # hoc.y depends on 9yacc which is not 100% yacc compatible
  else
  make -C $f DESTDIR="$butch_install_dir" install
  fi
done

cp "$K"/ssam "$butch_install_dir""$butch_prefix"/bin
ln -sf troff "$butch_install_dir""$butch_prefix"/bin/nroff
